
import React, { Component } from 'react';
import { Image, AsyncStorage } from 'react-native';
import { connect } from 'react-redux';
import { Container, Content, Item, Input, Button, Icon, View, Text } from 'native-base';
import { Actions } from 'react-native-router-flux';

import { setUser, login } from '../../actions/user';
import styles from './styles';


const background = require('../../../images/shadow.png');

class Login extends Component {

  static propTypes = {
    setUser: React.PropTypes.func,
  }

  constructor(props) {
    super(props);
    this.state = {
      name: '',
    };
    console.log('构造----')
  }

  async componentDidMount() {
    let user = await AsyncStorage.getItem("user");
    console.log(JSON.parse(user));

    if (!!user) {
      let userObj = JSON.parse(user);
      this.setState({
        name: userObj.name
      });
    }

  }

  Login() {
    let { dispatch } = this.props;
    dispatch(login(this.state.name, ''));

  }

  render() {
    console.log('render----------')
    console.log(this.props)
    return (
      <Container>
        <View style={styles.container}>
          <Content>
            <Image source={background} style={styles.shadow}>
              <View style={styles.bg}>
                <Item style={styles.input}>
                  <Icon active name="person" />
                  <Input value={this.state.name} placeholder="邮箱" onChangeText={name => this.setState({ name })} />
                </Item>
                <Item style={styles.input}>
                  <Icon name="unlock" />
                  <Input
                    placeholder="密码"
                    secureTextEntry
                  />
                </Item>
                <Button full style={styles.btn} onPress={() => this.Login()}>
                  <Text>登录</Text>
                </Button>
              </View>
            </Image>
          </Content>
        </View>
      </Container>
    );
  }
}

function bindActions(dispatch) {
  return {
    setUser: name => dispatch(setUser(name)),
    login: login
  };
}


export default connect(null, bindActions)(Login);
